package com.ltmonitor.jt808.service.impl;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.ltmonitor.jt808.protocol.T808Message;

public class AckQueue {

	private Thread processRealDataThread;
	private ConcurrentLinkedQueue<T808Message> dataQueue = new ConcurrentLinkedQueue();
	
	private ConcurrentHashMap simNoMap = new ConcurrentHashMap();
	
	private int maxVehicleNum;
	private int vehicleNum;
	
	private boolean started;
	
	public AckQueue(Thread t, int maxNum)
	{
		processRealDataThread = t;
		this.vehicleNum = 0;
		maxVehicleNum = maxNum;
	}
	
	public boolean isFull()
	{
		return getVehicleNum() > maxVehicleNum;
	}
	
	
	public boolean contains(String simNo)
	{
		return this.simNoMap.containsKey(simNo);
	}
	public void enQueue(T808Message tm) {
		int msgType = tm.getMessageType();
		String simNo = tm.getHeader().getSimId();
		if (simNoMap.containsKey(simNo) == false) {
			simNoMap.put(simNo, 1);
			this.vehicleNum ++;
		}

		dataQueue.add(tm);
		
		if(started == false)
		{
			processRealDataThread.start();
			started = true;
		}
	}
	
	public void stop()
	{
		try
		{
			processRealDataThread.join();
		}catch(Exception ex)
		{
			
		}
	}

	public ConcurrentLinkedQueue<T808Message> getDataQueue() {
		return dataQueue;
	}

	public int getVehicleNum() {
		return vehicleNum;
	}
	
}
